<?php

namespace backend\models\organization;

use bc\ar\MActiveRecord;
use backend\models\organization\OrganizationModel;

class ApplyModel extends MActiveRecord
{
	public static function tableName()
	{
		return '{{%organization_apply}}';
	}
	
	/**
	 * 
	 * @param unknown $params
	 * @param string $page
	 * @param string $pageSize
	 * @param string $oderBy
	 */
	public function getApplyList($params, $page = null, $pageSize = null, $oderBy = null)
	{
		$ar = $this->find ()->select ( '*' )
							->leftJoin ( OrganizationModel::getTableSchema ()->name, self::getTableSchema ()->name . '.organization_id=' . OrganizationModel::getTableSchema ()->name . '.id' );
		if ($params)
		{
			$ar->where ( $params );
		}
		if ($page && $pageSize)
		{
			$offset = $this->getOffset ( $page, $pageSize );
			$ar->offset ( $offset )->limit ( $pageSize );
		}
		
		if ($this->sort != null)
			$ar->orderBy ( $oderBy );
// 		$ar->groupBy ( 'id' );
		return $ar->asArray ()->all ();
	}
	
	/**
	 * 
	 */
	public function getApplyTotal($condition)
	{
		if(!empty($condition))
		{
			$ar = $this->find ()->select ( self::getTableSchema ()->name . '.*' )
			->leftJoin ( OrganizationModel::getTableSchema ()->name, self::getTableSchema ()->name . '.organization_id=' . OrganizationModel::getTableSchema ()->name . '.id' );
			if ($condition)
			{
				$ar->where ( $condition );
			}
// 			$ar->groupBy('id');
			return $ar->count();
		}
		else
			return 0;
	}
}

